package com.yunze.system.bill;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.yunze.common.mapper.mysql.YzCardFlowMapper;
import com.yunze.common.mapper.mysql.YzExecutionTaskMapper;
import com.yunze.common.utils.yunze.WriteCSV;
import com.yunze.common.utils.yunze.exportExclCZC;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 账单 导出
 * */
@Slf4j
@Component
public class ExportBill {

    @Resource
    private YzExecutionTaskMapper yzExecutionTaskMapper;
    @Resource
    private WriteCSV writeCSV;
    @Resource
    private YzCardFlowMapper yzCardFlowMapper;


    @RabbitHandler
    @RabbitListener(queues = "admin_exportBill_queue")
    public void exportBillTask(String msg) throws IOException {
        try {
            if (StringUtils.isEmpty(msg)) {
                return;
            }
            Map<String,Object> Pmap = JSON.parseObject(msg); // pmap = omap
            List<Map<String,Object>> outList = (List<Map<String, Object>>) Pmap.get("OutList");//往Excl中插入的数据
            Pmap.remove("OutList");//删除list
            String newName = Pmap.get("newName").toString();

            exportExclCZC.exportBill(outList, newName);//导出
            yzExecutionTaskMapper.set_end_time(Pmap);// 结束任务
            log.info("账单导出成功");
        }catch (Exception e){
            log.error(">>错误 - 客户资费导出 消费者:{}<<", e.getMessage().toString());
        }

    }

//    private void OutDataCard(Map<String, Object> map, Map<String, Object> user, List<Map<String, Object>> outCardIccidArr, String newName, Map<String, Object> task_map) {
//
//        Map<String, Object> Dept = (Map<String, Object>) user.get("dept");
//        String  agent_id = Dept.get("deptId").toString();
//        if(agent_id.equals("100")){
//            yzCardFlowMapper.queryPackage_simple(map);
//        }else{
//            Map<String,Object> PackageMap = new HashMap<String,Object>();
//            PackageMap.put("agent_id",agent_id);
//            yzCardFlowMapper.queryAgentPackage_simple(PackageMap);
//        }
//        map.put("iccid_arrs",outCardIccidArr);
//        try {
//            WriteOutCard(outCardIccidArr,newName,task_map);
//        }catch (Exception e){
//            log.error("导出客户资费异常 [导出数据 Exception] "+e.getCause().toString() );
//        }
//    }

//    private String WriteOutCard(List<Map<String, Object>> outCardIccidArr, String newName, Map<String, Object> task_map) {
//
//
//
//        writeCSV.Write(newName,outCardIccidArr,Outcolumns,null,keys);
//        yzExecutionTaskMapper.set_end_time(task_map);//任务结束
//        return "已下发执行日志可在【系统管理】》【日志管理】》【执行日志】查看";
//    }

}




























